// 杯子类
var Cup = (function(){
	var cup = function(options){
		var s = this,config = {
			
		};
		$.extend(config,options);
		// 杯子类型 30g 70g 100g
		s.type = config.type;
		// 显示位置
		s.x = config.x;
		s.y = config.y;
		s.w = config.w;
		s.h = config.h;
		// 刻度
		s.graduate = config.graduate;
		// 满刻度
		s.fillGraduate = config.fillGraduate;
		// 每一克的高度
		s.offset1g = config.offset1g;
		// 杯子当前水的高度
		s.waterH = config.graduate * config.offset1g;

		// 参考杯子  水的 水平竖直偏移
		s.waterSx = config.waterSx;
		s.waterSy = config.waterSy;
		// 杯子容器宽度、高度
		s.waterSw = config.waterSw;
		s.waterSh = config.waterSh;
		// 参考杯子  字体的 水平竖直偏移
		s.fontSx = config.fontSx;
		s.fontSy = config.fontSy;
		
		// 自身偏移
		s.sx = 0;
		s.sy = 0;
		// 是否移动
		s.isMove = false;
		// 2d对象
		s.render = config.render;
	};
	cup.prototype = {
		down : function(offsetX,offsetY){
			var s = this;
			s.isMove = true;

			s.sx = Math.abs(offsetX-s.x);
			s.sy = Math.abs(offsetY-s.y);

			s.setWaterRectByGraduate();
			s.renderCupByGraduate();
		},
		move : function(offsetX,offsetY){
			var s = this;

			s.x = offsetX - s.sx;
			s.y = offsetY - s.sy;

			s.setWaterRectByGraduate();
			s.renderCupByGraduate();
		},
		up : function(){
			var s = this;
			s.isMove = false;
		},
		// 根据刻度设置水的区域
		setWaterRectByGraduate : function(){
			var s = this,
			graduate = s.graduate,
			offset1g = s.offset1g;
			// 重置水的高度
			s.waterH = offset1g*graduate;
			// 水的绘制 点 描述
			// 左上角
			s.waterLTX = s.x + s.waterSx;
			s.waterLTY = s.y + s.waterSy + s.waterSh - s.waterH;
			// // 左下角
			s.waterLBX = s.waterLTX;
			s.waterLBY = s.waterLTY + s.waterH;
			// // 右下角
			s.waterRBX = s.waterLTX + s.waterSw;
			s.waterRBY = s.waterLBY;
			// // 右上角
			s.waterRTX = s.waterRBX;
			s.waterRTY = s.waterLTY;
		},
		// 根据刻度绘制杯子 【存在手掌】
		renderCupByGraduate : function(){
			var s = this,
			render = s.render,
			graduate = s.graduate,
			offset1g = s.offset1g,
			type = s.type,
			x = s.x,y = s.y,w = s.w,h = s.h;
			render.clearRect(0,0,940,605);
			render.save();
			render.drawImage(resources[type+'_z1'],x,y,w,h);
			render.drawImage(resources[type+'_z2'],x,y,w,h);
			if( graduate > 0 ){
				
				render.fillStyle = '#2a81b9';
				render.beginPath();
				render.moveTo(s.waterLTX,s.waterLTY);
				render.lineTo(s.waterLBX,s.waterLBY);
				render.lineTo(s.waterRBX,s.waterRBY);
				render.lineTo(s.waterRTX,s.waterRTY);
				render.closePath();
				render.fill();
				
			}
			render.drawImage(resources[type+'_z3'],x,y,w,h);
			render.drawImage(resources[type+'_z4'],x,y,w,h);
			render.fillStyle = '#ffffff';
			render.font='bolder 30px Microsoft YaHei';
			render.fillText(Math.ceil(graduate)+'g',x+s.fontSx,y+s.fontSy);
			render.restore();
		},
		// 根据刻度绘制杯子 【不存在手掌】
		renderCupByGraduate2 : function(){
			var s = this,
			render = s.render,
			graduate = s.graduate,
			offset1g = s.offset1g,
			type = s.type,
			x = s.x,y = s.y,w = s.w,h = s.h;
			render.clearRect(0,0,940,605);
			render.save();
			render.drawImage(resources[type+'_z2'],x,y,w,h);
			if( graduate > 0 ){
				
				render.fillStyle = '#2a81b9';
				render.beginPath();
				render.moveTo(s.waterLTX,s.waterLTY);
				render.lineTo(s.waterLBX,s.waterLBY);
				render.lineTo(s.waterRBX,s.waterRBY);
				render.lineTo(s.waterRTX,s.waterRTY);
				render.closePath();
				render.fill();
			}
			render.drawImage(resources[type+'_z3'],x,y,w,h);
			render.fillStyle = '#ffffff';
			render.font='bolder 30px Microsoft YaHei';
			render.fillText(Math.ceil(graduate)+'g',x+s.fontSx,y+s.fontSy);
			render.restore();
		},
		// 根据刻度绘制杯子 【存在3个点】
		renderCupByGraduate3 : function(){
			var s = this,
			render = s.render,
			graduate = s.graduate,
			offset1g = s.offset1g,
			type = s.type,
			x = s.x,y = s.y,w = s.w,h = s.h;
			render.clearRect(0,0,940,605);
			render.save();
			render.drawImage(resources[type+'_z1'],x,y,w,h);
			render.drawImage(resources[type+'_z2'],x,y,w,h);
			if( graduate > 0 ){
				
				render.fillStyle = '#2a81b9';
				render.beginPath();
				render.moveTo(s.waterLTX,s.waterLTY);
				render.lineTo(s.waterLBX,s.waterLBY);
				render.lineTo(s.waterRBX,s.waterRBY);
				render.closePath();
				render.fill();
			}
			render.drawImage(resources[type+'_z3'],x,y,w,h);
			render.drawImage(resources[type+'_z4'],x,y,w,h);
			render.fillStyle = '#ffffff';
			render.font='bolder 30px Microsoft YaHei';
			render.fillText(Math.ceil(graduate)+'g',x+s.fontSx,y+s.fontSy);
			render.restore();
		},
		// 根据刻度绘制杯子 【无刻度，倒完的情况】
		renderCupByGraduate4 : function(){
			var s = this,
			render = s.render,
			graduate = s.graduate,
			offset1g = s.offset1g,
			type = s.type,
			x = s.x,y = s.y,w = s.w,h = s.h;
			render.clearRect(0,0,1000,605);
			render.save();
			render.drawImage(resources[type+'_z1'],x,y,w,h);
			render.drawImage(resources[type+'_z2'],x,y,w,h);
			render.drawImage(resources[type+'_z3'],x,y,w,h);
			render.drawImage(resources[type+'_z4'],x,y,w,h);
			render.fillStyle = '#ffffff';
			render.font='bolder 30px Microsoft YaHei';
			render.fillText(Math.ceil(graduate)+'g',x+s.fontSx,y+s.fontSy);
			render.restore();
		},
		// 返回(x,y)缓动动画
		tweenAnim : function(x,y,callback){
			var s = this;
			
			// 规定 50 次
			var t = 50;
			var tx = (x - s.x)/t;
			var ty = (y - s.y)/t;
			var timer = setInterval(function(){
				s.x += tx;
				s.y += ty;
				if(--t<=0){
					clearInterval(timer);
					s.x = x;
					s.y = y;
					s.setWaterRectByGraduate();
					s.renderCupByGraduate2();
					
					callback && callback();
				}else{
					s.setWaterRectByGraduate();
					s.renderCupByGraduate();
				}
			},1000/60);
		},
		// 完全倒入
		pourComplete : function(cup,callback){
			var s = this,
			render = s.render,
			fillGraduate = s.fillGraduate,
			graduate = s.graduate;

			// 记录 等待+倒入度数
			s.du = 90;

			render.save();

			// 判断 杯子全满的时候倒完
			if( fillGraduate == graduate ){
				// 完全倒入 / 等待度数+倒入度数
				var rbdu2 = (s.waterH + s.waterSw) / 90;
				// 临时存储需要增加水的刻度
				var tempgraduate = cup.graduate + graduate;
				// 算出倒入杯子需要的上升距离
				var cupupd = graduate / 90;

				var waterAnim = [
					[0,0,96,120],[96,0,97,120],
					[0,258,96,120],[96,258,96,120],
					[0,516,96,120],[96,516,96,120]
				],waterIndex = 0,r = 1,timer = setInterval(function(){
					render.clearRect(0,0,940,605);

					render.translate(s.x+20,s.y+20);
					render.rotate(-Math.PI/180);
					render.translate(-(s.x+20),-(s.y+20));

					if(++r<90){
						if(s.waterRTY >= s.waterRBY){
							if(s.waterRBX <= s.waterLBX){
								s.waterRBX = s.waterLBX;
								s.renderCupByGraduate4();
							}else{
								s.waterRBX = s.waterRBX - rbdu2;
								s.renderCupByGraduate3();
							}
						}else{
							s.waterRTY += rbdu2;
							s.renderCupByGraduate();
						}
						cup.graduate += cupupd;
						cup.setWaterRectByGraduate();
						cup.renderCupByGraduate2();

						if(++waterIndex>=6){
							waterIndex = 0;
						}

						var arr = waterAnim[waterIndex];

						water_2d.clearRect(0,0,940,605);
						water_2d.drawImage(resources['water'],arr[0],arr[1],arr[2],arr[3],s.x-arr[2]+20,s.y+20,arr[2],arr[3]);
					}else{
						clearInterval(timer);

						water_2d.clearRect(0,0,940,605);

						cup.graduate = tempgraduate;
						cup.setWaterRectByGraduate();
						cup.renderCupByGraduate2();

						s.graduate = 0;
						s.setWaterRectByGraduate();
						s.renderCupByGraduate();

						s.returnPourAnim2(callback);
					}
				},1000/60);
			}else{
				// 等待度数
				var du1 = Math.floor( (fillGraduate - graduate)*90/fillGraduate );
				// 倒入度数
				var du2 = 90 - du1;
				// 左上角距离临界点的距离 / 等待度数
				var watertop = s.y + s.waterSy;
				var ltdu1 = (s.waterSh - s.waterH) / du1;
				// 完全倒入 / 等待度数+倒入度数
				var rbdu2 = (s.waterH + s.waterSw) / 90;
				// 临时存储需要增加水的刻度
				var tempgraduate = cup.graduate + graduate;
				// 算出倒入杯子需要的上升距离
				var cupupd = graduate / du2;

				var waterAnim = [
					[0,0,96,120],[96,0,97,120],
					[0,258,96,120],[96,258,96,120],
					[0,516,96,120],[96,516,96,120]
				],waterIndex = 0,r = 1,timer = setInterval(function(){
					render.clearRect(0,0,940,605);

					render.translate(s.x+20,s.y+20);
					render.rotate(-Math.PI/180);
					render.translate(-(s.x+20),-(s.y+20));

					if(++r<du1){
						// 等待
						if( (s.waterLTY -= ltdu1) < watertop ){
							s.waterLTY = watertop;
						}
						if(s.waterRTY >= s.waterRBY){
							if(s.waterRBX <= s.waterLBX){
								s.waterRBX = s.waterLBX;
								s.renderCupByGraduate4();
							}else{
								s.waterRBX = s.waterRBX - rbdu2;
								s.renderCupByGraduate3();
							}
						}else{
							s.waterRTY += rbdu2;
							s.renderCupByGraduate();
						}
					}else if(r<90){
						if(s.waterRTY >= s.waterRBY){
							if(s.waterRBX <= s.waterLBX){
								s.waterRBX = s.waterLBX;
								s.renderCupByGraduate4();
							}else{
								s.waterRBX = s.waterRBX - rbdu2;
								s.renderCupByGraduate3();
							}
						}else{
							s.waterRTY += rbdu2;
							s.renderCupByGraduate();
						}

						cup.graduate += cupupd;
						cup.setWaterRectByGraduate();
						cup.renderCupByGraduate2();

						if(++waterIndex>=6){
							waterIndex = 0;
						}

						var arr = waterAnim[waterIndex];

						water_2d.clearRect(0,0,940,605);
						water_2d.drawImage(resources['water'],arr[0],arr[1],arr[2],arr[3],s.x-arr[2]+20,s.y+20,arr[2],arr[3]);
						}else{
							clearInterval(timer);
							
							water_2d.clearRect(0,0,940,605);

							cup.graduate = tempgraduate;
							cup.setWaterRectByGraduate();
							cup.renderCupByGraduate2();

							s.graduate = 0;
							s.setWaterRectByGraduate();
							s.renderCupByGraduate();

							s.returnPourAnim2(callback);
						}
				},1000/60);
			}
		},
		// 未完全倒入
		unPourComplete : function(n,cup,callback){
			var s = this,
			render = s.render,
			fillGraduate = s.fillGraduate,
			graduate = s.graduate;

			// 等待度数
			var du1 = Math.floor( (fillGraduate - graduate)*90/fillGraduate );
			// 倒入度数
			var du2 = Math.floor( n*90/fillGraduate );
			// 等待+倒入度数
			var du3 = du1 + du2;
			// 左上角距离临界点的距离 / 等待度数
			var watertop = s.y + s.waterSy;
			var ltdu1 = (s.waterLTY - watertop) / du1;
			// 未完全倒入 / 等待度数+倒入度数
			var rbdu1 = s.waterH / du3;
			// 临时存储需要增加水的刻度
			var tempgraduate = cup.graduate + n;
			// 算出倒入杯子需要的上升距离
			var cupupd = n / du2;

			// 记录 等待+倒入度数
			s.du = du3;
			// 重置 刻度
			s.graduate = graduate - n;

			render.save();

			var waterAnim = [
				[0,0,96,120],[96,0,97,120],
				[0,258,96,120],[96,258,96,120],
				[0,516,96,120],[96,516,96,120]
			],waterIndex = 0,r = 1,timer = setInterval(function(){
				render.clearRect(0,0,940,605);

				render.translate(s.x+20,s.y+20);
				render.rotate(-Math.PI/180);
				render.translate(-(s.x+20),-(s.y+20));
				if(++r<du1){
					// 等待
					if( (s.waterLTY -= ltdu1) < watertop ){
						s.waterLTY = watertop;
					}
					s.waterRTY += rbdu1;
				}else if(r<du3){
					if(s.waterRTY >= s.waterRBY){
						s.waterRTY = s.waterRBY;
					}else{
						s.waterRTY += rbdu1;
					}

					cup.graduate += cupupd;
					cup.setWaterRectByGraduate();
					cup.renderCupByGraduate2();

					if(++waterIndex>=6){
						waterIndex = 0;
					}

					var arr = waterAnim[waterIndex];

					water_2d.clearRect(0,0,940,605);
					water_2d.drawImage(resources['water'],arr[0],arr[1],arr[2],arr[3],s.x-arr[2]+20,s.y,arr[2],arr[3]);
				}else{
					clearInterval(timer);

					water_2d.clearRect(0,0,940,605);

					// 重绘
					cup.graduate = tempgraduate;
					cup.setWaterRectByGraduate();
					cup.renderCupByGraduate2();

					s.returnPourAnim(callback);
				}

				s.renderCupByGraduate();
			},1000/60);
		},
		// 倒水返回动画【未倒完】
		returnPourAnim : function(callback){
			var s = this,
			render = s.render,
			graduate = s.graduate,
			offset1g = s.offset1g;
			// 重置水的高度
			s.waterH = offset1g*graduate;
			var ltdu1 = (s.waterSh - s.waterH) / s.du;
			var rtdu1 = s.waterH / s.du;
			s.waterRTX = s.waterRBX = s.waterLBX + s.waterSw;
			s.waterRTY = s.waterRBY = s.waterLBY;
			
			var r = s.du,timer = setInterval(function(){
				render.clearRect(0,0,940,605);

				render.translate(s.x+20,s.y+20);
				render.rotate(Math.PI/180);
				render.translate(-(s.x+20),-(s.y+20));

				s.waterLTY += ltdu1;
				s.waterRTY -= rtdu1;

				s.renderCupByGraduate();

				if(--r<=0){
					clearInterval(timer);
					render.restore();
					s.setWaterRectByGraduate();
					s.renderCupByGraduate();
					callback && callback();
				}
			},1000/60);
		},
		// 倒水返回动画【倒完】
		returnPourAnim2 : function(callback){
			var s = this,
			render = s.render;
			
			var r = s.du,timer = setInterval(function(){
				render.clearRect(0,0,940,605);

				render.translate(s.x+20,s.y+20);
				render.rotate(Math.PI/180);
				render.translate(-(s.x+20),-(s.y+20));

				s.renderCupByGraduate4();

				if(--r<=1){
					clearInterval(timer);
					render.restore();
					callback && callback();
				}
			},1000/60);
		},
		resetParam : function(options){
			var s = this,config = {
				
			};
			$.extend(config,options);
			s.x = config.x;
			s.y = config.y;
			s.w = config.w;
			s.h = config.h;
			s.graduate = config.graduate;
			s.fillGraduate = config.fillGraduate;
			s.waterSx = config.waterSx;
			s.waterSy = config.waterSy;
			s.waterSw = config.waterSw;
			s.waterSh = config.waterSh;
			s.fontSx = config.fontSx;
			s.fontSy = config.fontSy;
		}
	};
	return cup;
})();